﻿using DesktopLauncher.Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;

namespace DesktopLauncher.Helper
{
    /// <summary>
    /// 文件信息操作类.
    /// </summary>
    class FileInfoHelper
    {
        public string FolderPath { get; set; }
        IconHelper iconHelper = new IconHelper();
        /// <summary>
        /// 从文档中读取文件信息
        /// </summary>
        /// <returns>如果出现异常,返回空</returns>
        public List<FileInfoModel> GetCachedFileInfo()
        {
            List<FileInfoModel> ret = null;
            try
            {
                ret = LoadFileInfos();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return ret;
        }
        /// <summary>
        /// 重新读取图标信息,然后写入存档文件中.
        /// </summary>
        /// <param name="h">窗口句柄.......</param>
        /// <param name="files">要读取的文件名(不需要完整路径,因为文件夹地址已经保存在this.FolderPath中了)</param>
        /// <returns></returns>
        public List<FileInfoModel> ReadAndCacheFileInfo(IntPtr h, string[] files)
        {
            List<FileInfoModel> list = new List<FileInfoModel>();
            foreach (string fileName in files)
            {
                String filePath = Path.Combine(this.FolderPath, fileName);
                list.Add(new FileInfoModel()
                {
                    FileName = fileName,
                    FilePath = filePath,
                    Ext = Path.GetExtension(fileName),
                    Icon = iconHelper.GetfileExtIcon(h, filePath).ToBitmap(),            
                    FileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName),
                });
            }
            //把文件信息保存
            try
            {
                SaveFileInfos(list);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 比较读取的文件列表和缓存的文件实体列表是否匹配.
        /// 先比较数目,然后具体比较每一个文件名.都匹配才返回true
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileModels"></param>
        /// <returns></returns>
        public static bool CompareFileInfo(string[] filePath, List<FileInfoModel> fileModels)
        {
            if (filePath == null || fileModels == null)
                return false;
            if (filePath.Length != fileModels.Count)
                return false;
            foreach (var fm in fileModels)
            {
                if (!filePath.Contains(fm.FileName))
                    return false;
            }
            return true;
        }

        public static string[] GetFileNames(string[] filePath)
        {
            string[] fileNames = new string[filePath.Length];
            for (int i = 0; i < fileNames.Length; i++)
            {
                fileNames[i] = Path.GetFileName(filePath[i]);
            }
            return fileNames;
        }

        private static void SaveFileInfos(List<FileInfoModel> list)
        {
            string filePath = Path.Combine(Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName), "FileCache.bin");
            using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
            {
                BinaryFormatter b = new BinaryFormatter();
                b.Serialize(fileStream, list);
            }
        }

        private static List<FileInfoModel> LoadFileInfos()
        {
            string filePath = Path.Combine(Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName), "FileCache.bin");
            List<FileInfoModel> items = null;
            using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                BinaryFormatter b = new BinaryFormatter();
                items = b.Deserialize(fileStream) as List<FileInfoModel>;
            }
            return items;
        }
    }
}
